<!--
 * @Author: your name
 * @Date: 2021-11-09 20:51:22
 * @LastEditTime: 2021-11-10 20:46:49
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEl
 * @FilePath: \li-yuwei\admin\src\views\login\index.vue
-->
<template>
  <div class="login">
    <div class="login-box">
      <div class="left-login-box">
        <img :src="registerImg" alt="" />
      </div>
      <div class="right-login-box">
        <div class="login-main">
          <div class="login-title">访客注册</div>
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账户" prop="username">
              <el-input v-model.number="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="确认" prop="checkPass">
              <el-input
                type="password"
                v-model="ruleForm.checkPass"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="login-btn-box">
            <el-button type="primary" @click="submitForm()">注册</el-button>
          </div>
          <div class="or-box" @click="goLogin()">Or 去登录</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import registerImg from "../../assets/注册.webp";
import api from "../../api/index";
export default {
  data() {
    var checkUser = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账户不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      registerImg,
      ruleForm: {
        pass: "",
        checkPass: "",
        username: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur", required: true }],
        checkPass: [
          { validator: validatePass2, trigger: "blur", required: true },
        ],
        username: [{ validator: checkUser, trigger: "blur", required: true }],
      },
    };
  },
  methods: {
    submitForm() {
      if (
        this.ruleForm.checkPass === this.ruleForm.pass &&
        this.ruleForm.checkPass &&
        this.ruleForm.username
      ) {
        api
          .register({
            username: this.ruleForm.username,
            password: this.ruleForm.pass,
            nickname: this.ruleForm.username,
          })
          .then((res) => {
            if (res.message === "操作成功") {
              this.$message({
                message: "注册成功",
                type: "success",
              });
              this.goLogin();
            } else {
              this.$message({
                message: "账户已存在",
                type: "warning",
              });
            }
          });
      }
    },

    goLogin() {
      this.$router.push("/login");
    },
  },
  created() {},
};
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #e7eaee;
}
.login-box {
  width: 85%;
  height: 85%;
  background-color: #fff;
  display: flex;
}
.left-login-box {
  height: 100%;
  img {
    height: 100%;
  }
}
.right-login-box {
  flex: 1;
  background-color: #f7fafb;
  display: flex;
  align-items: center;
  .login-main {
    width: 100%;
    padding: 0 20px;
  }
}
.login-title {
  margin-left: 50px;
  margin-bottom: 20px;
  font-size: 20px;
}
.login-btn-box {
  width: 100%;
  display: flex;
  justify-content: center;
  button {
    width: 94%;
    margin-left: 48px;
  }
}
.or-box {
  font-size: 12px;
  font-weight: normal;
  margin-left: 48px;
  margin-top: 10px;
  cursor: pointer;
}
</style>
